package com.curvebreakers.wheresmystuff.activities;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.*;

import android.os.*;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.*;

public class EditProfileActivity extends AuthorizedActivity implements DBResultListener {
    private ProgressDialog progressDialog;

	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_editprofile);
		
		final User u = Session.getSession().getUser();
		final EditText email = (EditText) findViewById(R.id.regEmail);
		final CheckBox passwordChange = (CheckBox) findViewById(R.id.passwordChange);
		final View passwordChangeContainer = findViewById(R.id.passwordChangeContainer);
		final EditText password = (EditText) findViewById(R.id.regPassword);
		final EditText confirmPassword = (EditText) findViewById(R.id.regConfirmPassword);
		final EditText phoneNum = (EditText) findViewById(R.id.regPhoneNum);
	    email.setText(u.getEmail());
	    phoneNum.setText(u.getPhoneNum());
	    
	    passwordChange.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					passwordChangeContainer.setVisibility(View.VISIBLE);
				} else {
					passwordChangeContainer.setVisibility(View.GONE);
				}
			}
		});
	    
	    final DBResultListener listener = this;
	    final Button button = (Button) findViewById(R.id.submitChangesButton);
        button.setOnClickListener(new View.OnClickListener() {           
	        public void onClick(View v) {
	        	// Validate the form
	        	boolean valid = true;
	        	
	        	if (passwordChange.isChecked()) {
		        	if (password.getText().toString().length() < 8) {
		        		password.setError("Passwords must be at least 8 character");
		        		valid = false;
		        	} else if (!password.getText().toString().equals(confirmPassword.getText().toString())) {
		        		confirmPassword.setError("Passwords do not match");
		        		valid = false;
		        	}
	        	}
	        	
	        	if (valid) {
		            progressDialog = new ProgressDialog(EditProfileActivity.this, ProgressDialog.THEME_TRADITIONAL);//ProgressDialog.show(getActivity(), "Validating login...", "Please Wait");
		            progressDialog.setTitle("Editing your profile...");
		            progressDialog.setMessage("Please Wait");
		            progressDialog.show();
		            
		        	u.setEmail(email.getText().toString());
		        	if (passwordChange.isChecked() && password.getText().toString().equals(confirmPassword.getText().toString()))
			        	u.setPassword(password.getText().toString());
		        	u.setPhoneNum(phoneNum.getText().toString());
		            DatabaseModel.saveUser(u, listener);
	            }
	        }
        });
        final Button cancelButton = (Button) findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener() {           
	        public void onClick(View v) {
	        	EditProfileActivity.this.finish();
	        }
        });
	}
	
	
	@Override
	public void onDBResultReceived(Object obj) {
		progressDialog.dismiss();
		Session.getSession().login((User)obj);
		finish();
	}

	@Override
	public void onDBRequestFailure(String message) {
		progressDialog.dismiss();
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

}
